﻿using LightCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightCAD.UI
{
    public partial class CommandBar : UserControl, ICommandControl
    {
        private event Action<KeyEventRuntime> inputKeyDown;
        private event Action<KeyEventRuntime> inputKeyUp;
        private List<string> allCommonds;
        private List<string> cmdHistory;

        public CommandBar()
        {
            InitializeComponent();
            this.txtInput.KeyDown += TxtInput_KeyDown;
            this.txtInput.KeyUp += TxtInput_KeyUp;

        }

        private void TxtInput_KeyUp(object? sender, KeyEventArgs e)
        {
            inputKeyUp?.Invoke(e.ToRuntime());
        }

        private void TxtInput_KeyDown(object? sender, KeyEventArgs e)
        {
            if (this.lbCmds.Visible) //有备选命令
            {
                if (e.KeyCode == Keys.Up)
                {
                    this.lbCmds.SelectedIndex = Math.Max(0, this.lbCmds.SelectedIndex - 1);
                }
                if (e.KeyCode == Keys.Down)
                {
                    this.lbCmds.SelectedIndex = Math.Min(this.lbCmds.Items.Count - 1, this.lbCmds.SelectedIndex + 1);
                }
                if (e.KeyCode == Keys.Enter)
                {
                    var cmd = (string)this.lbCmds.SelectedItem;
                    if (!string.IsNullOrEmpty(cmd))
                    {
                        this.txtInput.TextChanged -= txtInput_TextChanged;
                        this.txtInput.Text = cmd;
                        this.txtInput.TextChanged += txtInput_TextChanged;
                        this.inputKeyDown?.Invoke(new KeyEventRuntime() { KeyCode = (int)Keys.Enter, KeyModifiers = LcKeyModifiers.None });
                    }
                }
                return;
            }
            inputKeyDown?.Invoke(e.ToRuntime());
        }

        public void AttachInputEvent(Action<KeyEventRuntime> onInputKeyDown, Action<KeyEventRuntime> onInputKeyUp)
        {
            this.inputKeyDown += onInputKeyDown;
            this.inputKeyUp += onInputKeyUp;
        }

        public void DetachInputEvent(Action<KeyEventRuntime> onInputKeyDown, Action<KeyEventRuntime> onInputKeyUp)
        {
            this.inputKeyDown -= onInputKeyDown;
            this.inputKeyUp -= onInputKeyUp;
        }

        public void Prompt(string prompt)
        {
            this.lblPrompt.Text = prompt;
        }
        public string GetPrompt()
        {
            if (this.lblPrompt.Text == null || this.lblPrompt.Text.Length == 0)
                return "命令：";
            else
                return this.lblPrompt.Text;
        }
        public string GetInputText()
        {
            return this.txtInput.Text;
        }

        public void SetInputText(string str)
        {
            this.txtInput.Text = str;
        }

        public void WriteInfo(string info)
        {
            this.txtInfo.HideSelection = false;
            this.txtInfo.AppendText("\n" + info);
        }
        public string GetAllInfos()
        {
            return this.txtInfo.Text;
        }

        public void SetAllCommonds(List<string> cmds)
        {
            this.allCommonds = cmds;
        }

        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            this.lbCmds.Items.Clear();
            if (this.allCommonds == null || this.allCommonds.Count == 0)
            {
                this.lbCmds.Visible = false;
                return;
            }

            string inputeTxt = this.txtInput.Text.ToUpper();
            if (string.IsNullOrEmpty(inputeTxt))
            {
                this.lbCmds.Visible = false;
                return;
            }
            foreach (var cmd in this.allCommonds)
            {
                if (cmd.ToUpper().Contains(inputeTxt))
                {
                    this.lbCmds.Items.Add(cmd);
                }
            }
            if (this.lbCmds.Items != null && this.lbCmds.Items.Count > 0)
            {
                var location = new Point(this.txtInput.Location.X, this.lbCmds.Location.Y);

                this.lbCmds.Location = location;
                this.lbCmds.Visible = true;
                this.lbCmds.SelectedIndex = 0;
            }
            else
            {
                this.lbCmds.Visible = false;
            }

        }

        private void lbCmds_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.lbCmds.SelectedItem == null)
            {
                return;
            }

            var cmd = (string)this.lbCmds.SelectedItem;
            this.txtInput.TextChanged -= txtInput_TextChanged;
            this.txtInput.Text = cmd;
            this.txtInput.TextChanged += txtInput_TextChanged;
            this.inputKeyDown?.Invoke(new KeyEventRuntime() { KeyCode = (int)Keys.Enter, KeyModifiers = LcKeyModifiers.None });
        }
    }
}
